<?php


namespace app\common\repositories\system\merchant;


use app\common\dao\system\merchant\ArticleCategoryDao;
use app\common\repositories\BaseRepository;
use FormBuilder\Exception\FormBuilderException;
use FormBuilder\Factory\Elm;
use FormBuilder\Form;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Route;


/**
 * @mixin ArticleCategoryDao
 */
class ArticleCategoryRepository extends BaseRepository
{
    /**
     * ArticleCategoryRepository constructor.
     * @param ArticleCategoryDao $dao
     */
    public function __construct(ArticleCategoryDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param $status
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function getFormatList($status = null)
    {
        return formatCategory($this->dao->getAll($status)->toArray(), 'mer_article_category_id');
    }


    /**
     * @return \think\Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function apiGetArticleCategory()
    {
        return $this->dao->search(['status' => 1, 'pid' => 0])->select();
    }

    /**
     * @param int|null $id
     * @param array $formData
     * @return Form
     * @throws FormBuilderException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function form(?int $id = null, array $formData = [])
    {
        $form = Elm::createForm(is_null($id) ? Route::buildUrl('merchantArticleCategoryCreate')->build() : Route::buildUrl('merchanArticleCategoryUpdate', ['id' => $id])->build());
        $form->setRule([
            Elm::input('title', '分类名称')->required(),
            Elm::input('info', '分类简介'),
            Elm::frameImage('image', '分类图片', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=image&type=1')->width('1000px')->height('600px')->props(['footer' => false])->modal(['modal' => false, 'custom-class' => 'suibian-modal']),
            Elm::switches('status', '状态', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
            Elm::number('sort', '排序', 0)->precision(0)->max(99999),
        ]);
        return $form->setTitle(is_null($id) ? '添加文章配置' : '编辑文章分类')->formData($formData);
    }

    /**
     * @param $id
     * @return Form
     * @throws FormBuilderException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function updateForm($id)
    {
        return $this->form($id, $this->dao->get($id)->toArray());
    }
}
